<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 6/3/21
 * Time: 2:47 PM
 */

namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\model\Product;
use app\api\model\ProductOrder;
use app\api\model\ProductOrderContent;
use app\api\model\Store;
use app\api\model\StoreUserItem;
use app\api\model\UserInvitation;
use app\api\validate\CheckStoreUser;
use app\lib\exception\ParameterException;
use app\api\service\Token as TokenService;
use app\api\model\User as UserModel;
use app\api\model\StoreUser as StoreUserModel;
use app\api\model\UserMoney as UserMoneyModel;
use app\api\model\StoreGroup as StoreGroupModel;
use app\api\model\Team as TeamModel;

class StoreUser extends BaseController
{

    // 验证当前用户信息绑定员工
    public function checkStoreUser($scene)
    {
        $scene = explode("-",$scene);
        if($scene[0] != 'store') {
            throw new ParameterException([
                'msg' => [
                    'store' => '二维码信息错误!'
                ]
            ]);
        }
        $uid = TokenService::getCurrentUid();
        $userInfo = UserModel::get($uid);
        if($userInfo['type'] != 1){
            throw new ParameterException([
                'msg' => [
                    'store' => '您已经绑定其他用户类型!'
                ]
            ]);
        }
        $storeInfo = Store::get($scene[1]);
        if(!$storeInfo) {
            throw new ParameterException([
                'msg' => [
                    'store' => '该店面信息不存在!'
                ]
            ]);
        }
        return [ 'store_id' => $scene[1] ];
    }

    // 创建员工信息
    public function createData($store_id,$name,$phone,$desc,$group_id)
    {
        (new CheckStoreUser())->goCheck();
        $uid = TokenService::getCurrentUid();
        $userInfo = UserModel::get($uid);
        if($userInfo['type'] != 1){
            throw new ParameterException([
                'msg' => [
                    'store' => '您已经绑定其他用户类型!'
                ]
            ]);
        }
        $storeInfo = Store::get($store_id);
        if(!$storeInfo) {
            throw new ParameterException([
                'msg' => [
                    'store' => '该店面信息不存在!'
                ]
            ]);
        }
        $mode = StoreUserModel::create([
            'name' => $name,
            'phone' => $phone,
            'desc' => $desc,
            'store_id' => $store_id,
            'user_id' => $uid,
            'group_id'=>$group_id
        ]);
        UserModel::where('id',$uid)->update([
            'type' => 4,
            'store_id' => $store_id,
        ]);
        $this->setOldCode($mode->id);
        $this->setNewCode($mode->id);
        return writeJson(201, [], '绑定成功！');
    }


    /**
     * 生成旧用户分享二维码
     */
    public function setOldCode($id)
    {
        $data['scene'] = 'storeUserOld-'.$id;
        $data['width'] = 280;
        $name = 'user_old_'.$id;
        $token = new TokenService();
        $token->setCodePub($data,$name);
    }

    /**
     * 生成新用户分享二维码
     */
    public function setNewCode($id)
    {
        $data['scene'] = 'storeUserNew-'.$id;
        $data['width'] = 280;
        $name = 'user_new_'.$id;
        $token = new TokenService();
        $token->setCodePub($data,$name);
    }

    /**
     * 获取店面小组列表
     */
    public function getGroupList($id)
    {
        return StoreGroupModel::where('store_id',$id)->select()->toArray();
    }

    /**
     * 获取新旧二维码
     */
    public function getCodeList()
    {
        $uid = TokenService::getCurrentUid();
        $store_user = StoreUserModel::where('user_id',$uid)->find();
        return [
            'old_url' => config('setting.invite_img_prefix').'/user_old_'.$store_user['id'].'.png',
            'new_url' => config('setting.invite_img_prefix').'/user_new_'.$store_user['id'].'.png'
        ];
    }



    /**
     * 获取店员销售额+收入
     */
    public function getStaffList()
    {
        $uid = TokenService::getCurrentUid();
        $storeUser = StoreUserModel::where('user_id',$uid)->with('store')->find();
        if($storeUser) {
            $price_one = 0;
            $new_user_count = 0;
            $product_count = 0;
            $order_count = 0;
            $user_money = 0;
            $user_money_one = 0;
            $fission = 0;
            if($storeUser['type'] == 1) {
                /**
                 * 读取店铺内所有的成交数据
                 */
                $price = ProductOrder::where('store_id','eq',$storeUser['store_id'])->where('status','gt',1)->sum('actual_price');

                $invite_money = UserMoneyModel::where('user_id',$uid)
                    ->where('type',1)->sum('money');
                $userInvitationOne = UserInvitation::where('type',2)
                    ->where('inviter',$storeUser['id'])
                    ->select()->toArray();
                $ids = array_column($userInvitationOne,'invitee');
                $price_one = ProductOrder::where('user_id','in',$ids)
                    ->where('status','gt',1)
                    ->sum('actual_price');

                $storeUsers = StoreUserModel::where('store_id',$storeUser['store_id'])
                    ->select()->toArray();
                $ids = array_column($storeUsers,'user_id');
                $whereOne = [];
                $whereOne[] = ['user_id','in',$ids];
                $whereOne[] = ['type','eq',1];
                $user_money = UserMoneyModel::where($whereOne)->sum('money');

                $whereTwo = [];
                $whereTwo[] = ['id','in',$ids];
                $whereTwo[] = ['type','in',[2,3,4]];
                $whereTwo[] = ['store_id','eq',$storeUser['store_id']];
                $user_money_one = UserModel::where($whereTwo)->sum('money');

                $new_user_count = 0;
                $userInviteOne = UserInvitation::where('type',2)->select()->toArray();
                $idsOne = array_column($userInviteOne,'invitee');
                $whereFour = [];
                $whereFour[] = ['id','in',$idsOne];
                $whereFour[] = ['status','eq',3];
                $whereFour[] = ['new_type','eq',1];
                $whereFour[] = ['store_id','eq',$storeUser['store_id']];
                $userOne = UserModel::where($whereFour)->select()->toArray();
                foreach ($userOne as $ka=>$va){
                    $whereFive = [];
                    $whereFive[] = ['user_id','eq',$userOne[$ka]['id']];
                    $whereFive[] = ['status', 'gt', 1];
                    $order = ProductOrder::where($whereFive)->find();
                    if($order) {
                        $new_user_count += 1;
                    }
                }

                $storeUser = StoreUserModel::where('user_id',$uid)
                    ->with('user,store')->find();
                $product = Product::where('store_id',$storeUser['store_id'])
                    ->select()->toArray();
                $product_count = 0;
                foreach ($product as $v){
                    $product_count += ProductOrderContent::where('product_id','eq',$v['id'])
                        ->where('status','gt',1)->sum('product_number');
                }
                $order_count = ProductOrder::where('store_id','eq',$storeUser['store_id'])
                    ->where('status','gt',1)
                    ->count();

                $whereOne = [];
                $whereOne[] = ['user.status','eq',3];
                $whereOne[] = ['user.type','eq',1];
                $whereOne[] = ['user.store_id', 'eq', $storeUser['store_id']];
                $user = UserModel::hasWhere('invite',function($query){
                    $query->where('UserInvitation.type','eq',1);
                })->where($whereOne)->select()->toArray();
                $fission = 0;
                foreach ($user as $ka=>$va){
                    $whereTwo = [];
                    $whereTwo[] = ['user_id','eq',$user[$ka]['id']];
                    $whereTwo[] = ['status', 'gt', 1];
                    $order = ProductOrder::where($whereTwo)->find();
                    if($order) {
                        $fission = $fission +1;
                    }
                }
            }elseif($storeUser['type'] == 2){
                $storeUsers = StoreUserModel::where('group_id',$storeUser['group_id'])
                    ->with('user,store')->select()->toArray();
                $price = 0;
                $invite_money = 0;
                foreach ($storeUsers as $k=>$v){
                    $userInvitation = UserInvitation::where('type',2)
                        ->where('inviter',$v['id'])
                        ->select()->toArray();
                    $ids = array_column($userInvitation,'invitee');
                    $price += ProductOrder::where('user_id','in',$ids)->where('status','gt',1)->sum('actual_price');
                    $invite_money += UserMoneyModel::where('user_id',$uid)
                        ->where('type',1)->sum('money');
                }
                $userInvitationOne = UserInvitation::where('type',2)
                    ->where('inviter',$storeUser['id'])
                    ->select()->toArray();
                $ids = array_column($userInvitationOne,'invitee');
                $price_one = ProductOrder::where('user_id','in',$ids)
                    ->where('status','gt',1)
                    ->sum('actual_price');
                $storeUsers = StoreUserModel::where('group_id',$storeUser['group_id'])
                    ->with('user')->select()->toArray();
                $orderList = [];
                foreach ($storeUsers as $k=>$v){
                    $userInvitation = UserInvitation::where('type','eq',2)
                        ->where('inviter','eq',$v['id'])
                        ->select()->toArray();
                    $ids = array_column($userInvitation,'invitee');
                    $order = ProductOrder::where('user_id','in',$ids)
                        ->where('status','gt',1)
                        ->with('user,content')
                        ->select();
                    if(!$order->isEmpty()) {
                        foreach ($order->toArray() as $vo){
                            array_push($orderList,$vo);
                        }
                    }
                }
                $order_count = count($orderList);
            }elseif($storeUser['type'] == 3) {
                $userInvitation = UserInvitation::where('type',2)
                    ->where('inviter',$storeUser['id'])
                    ->select()->toArray();
                $ids = array_column($userInvitation,'invitee');
                $price = ProductOrder::where('user_id','in',$ids)
                    ->where('status','gt',1)
                    ->sum('actual_price');
                $invite_money = UserMoneyModel::where('user_id',$uid)
                    ->where('type',1)->sum('money');
                $whereOne = [];
                $whereOne[] = ['user.status','eq',3];
                $whereOne[] = ['user.type','eq',1];
                $whereOne[] = ['user.store_id', 'eq', $storeUser['store_id']];
                $user = UserModel::where($whereOne)->select()->toArray();
                $fission = 0;
                foreach ($user as $ka=>$va){
                    $whereTwo = [];
                    $whereTwo[] = ['user_id','eq',$user[$ka]['id']];
                    $whereTwo[] = ['status', 'gt', 1];
                    $order = ProductOrder::where($whereTwo)->find();
                    if(!$order) {
                        $fission = $fission +1;
                    }
                }
            } elseif($storeUser['type'] == 4){
                /**
                 * 读取下属店铺内所有的成交数据
                 */
                $storeUserItem = StoreUserItem::where('store_user_id',$storeUser['id'])->select()->toArray();
                $storeIds = array_column($storeUserItem,'store_id');
                $price = ProductOrder::where('store_id','in',$storeIds)->where('status','gt',1)->sum('actual_price');

                $invite_money = UserMoneyModel::where('user_id',$uid)
                    ->where('type',1)->sum('money');
                $userInvitationOne = UserInvitation::where('type',2)
                    ->where('inviter',$storeUser['id'])
                    ->select()->toArray();
                $ids = array_column($userInvitationOne,'invitee');
                $price_one = ProductOrder::where('user_id','in',$ids)
                    ->where('status','gt',1)
                    ->sum('actual_price');

                $storeUsers = StoreUserModel::where('store_id','in',$storeIds)
                    ->select()->toArray();
                $ids = array_column($storeUsers,'user_id');
                $whereOne = [];
                $whereOne[] = ['user_id','in',$ids];
                $whereOne[] = ['type','eq',1];
                $user_money = UserMoneyModel::where($whereOne)->sum('money');

                $whereTwo = [];
                $whereTwo[] = ['id','in',$ids];
                $whereTwo[] = ['type','in',[2,3,4]];
                $whereTwo[] = ['store_id', 'in', $storeIds];
                $user_money_one = UserModel::where($whereTwo)->sum('money');

                $new_user_count = 0;
                $userInviteOne = UserInvitation::where('type',2)->select()->toArray();
                $idsOne = array_column($userInviteOne,'invitee');
                $whereFour = [];
                $whereFour[] = ['id','in',$idsOne];
                $whereFour[] = ['status','eq',3];
                $whereFour[] = ['new_type','eq',1];
                $whereFour[] = ['store_id','in',$storeIds];
                $userOne = UserModel::where($whereFour)->select()->toArray();
                foreach ($userOne as $ka=>$va){
                    $whereFive = [];
                    $whereFive[] = ['user_id','eq',$userOne[$ka]['id']];
                    $whereFive[] = ['status', 'gt', 1];
                    $order = ProductOrder::where($whereFive)->find();
                    if($order) {
                        $new_user_count += 1;
                    }
                }

                $storeUser = StoreUserModel::where('user_id',$uid)
                    ->with('user,store')->find();
                $product = Product::where('store_id','in',$storeIds)
                    ->select()->toArray();
                $product_count = 0;
                foreach ($product as $k=>$v){
                    $product_count += ProductOrderContent::where('product_id','eq',$v['id'])
                        ->where('status','gt',1)->sum('product_number');
                }
                $order_count = ProductOrder::where('store_id','in',$storeIds)
                    ->where('status','gt',1)
                    ->count();

                $whereOne = [];
                $whereOne[] = ['user.status','eq',3];
                $whereOne[] = ['user.type','eq',1];
                $whereOne[] = ['user.store_id', 'in', $storeIds];
                $user = UserModel::hasWhere('invite',function($query){
                    $query->where('UserInvitation.type','eq',1);
                })->where($whereOne)->select()->toArray();
                $fission = 0;
                foreach ($user as $ka=>$va){
                    $whereTwo = [];
                    $whereTwo[] = ['user_id','eq',$user[$ka]['id']];
                    $whereTwo[] = ['status', 'gt', 1];
                    $order = ProductOrder::where($whereTwo)->find();
                    if($order) {
                        $fission = $fission +1;
                    }
                }
            }
            return [
                'price' => $price,
                'invite_money' => $invite_money,
                'price_one' => $price_one,
                'new_user_count' => $new_user_count,
                'product_count' => $product_count,
                'order_count' => $order_count,
                'user_money' => $user_money,
                'user_money_one' => $user_money_one,
                'store_user' => $storeUser,
                'fission' => $fission,
            ];
        } else {
            return [
                'price' => 0,
                'invite_money' => 0,
                'price_one' => 0,
                'new_user_count' => 0,
                'product_count' => 0,
                'order_count' => 0,
                'user_money' => 0,
            ];
        }
    }

    // 带裂变
    public function getFissionList()
    {
        $uid = TokenService::getCurrentUid();
        $storeUser = StoreUserModel::where('user_id',$uid)->with('store')->find();
        $whereOne = [];
        $whereOne[] = ['user.status','eq',3];
        $whereOne[] = ['user.type','eq',1];
        $whereOne[] = ['user.store_id', 'eq', $storeUser['store_id']];
//                $user = UserModel::hasWhere('invite',function($query){
//                    $query->where('UserInvitation.type','eq',1);
//                })->where($whereOne)->select()->toArray();
        $user = UserModel::where($whereOne)->select()->toArray();
        $fission = [];
        foreach ($user as $ka=>$va){
            $whereTwo = [];
            $whereTwo[] = ['user_id','eq',$user[$ka]['id']];
            $whereTwo[] = ['status', 'gt', 1];
            $order = ProductOrder::where($whereTwo)->find();
            if(!$order) {
                array_push($fission,$user[$ka]);
            }
        }
        return $fission;
    }

    /**
     * 获取店长和组长自己的销售额
     */
    public function getOwnerList()
    {
        $uid = TokenService::getCurrentUid();
        $storeUser = StoreUserModel::where('user_id',$uid)->with('user')->find();
        $userInvitation = UserInvitation::where('type','eq',2)
            ->where('inviter','eq',$storeUser['id'])
            ->with('user')
            ->select()->toArray();
        $array = [];
        foreach ($userInvitation as $k=>$v){
            $price = ProductOrder::where('user_id','eq',$v['invitee'])
                ->where('status','gt',1)
                ->sum('actual_price');
            $userInvitation[$k]['price'] = $price;
            if($price > 0){
                array_push($array,$userInvitation[$k]);
            }
        }
        $storeUser['rank'] = $array;
        return $storeUser;
    }

    /**
     * 销售额排行
     */
    public function getRankList()
    {
        $uid = TokenService::getCurrentUid();
        $storeUser = StoreUserModel::where('user_id',$uid)->with('user')->find();
        $store = Store::get($storeUser['store_id']);
        $array = [];
        if($storeUser['type'] == 1) {
            $storeUsers = StoreUserModel::where('store_id',$storeUser['store_id'])
                ->with('user')
                ->select()->toArray();
            foreach ($storeUsers as $k=>$v){
                $userInvitation = UserInvitation::where('type','eq',2)
                    ->where('inviter','eq',$v['id'])
                    ->select()->toArray();
                $ids = array_column($userInvitation,'invitee');
                $price = ProductOrder::where('user_id','in',$ids)->where('status','gt',1)->sum('actual_price');
                $storeUsers[$k]['price'] = $price;
                if($price > 0){
                    array_push($array,$storeUsers[$k]);
                }
            }
        } elseif($storeUser['type'] == 2) {
            $store = StoreGroupModel::where('id',$storeUser['group_id'])->find();
            $storeUsers = StoreUserModel::where('group_id',$storeUser['group_id'])
                ->with('user')
                ->select()
                ->toArray();
            foreach ($storeUsers as $k=>$v){
                $userInvitation = UserInvitation::where('type','eq',2)
                    ->where('inviter','eq',$v['id'])
                    ->select()->toArray();
                $ids = array_column($userInvitation,'invitee');
                $price = ProductOrder::where('user_id','in',$ids)
                    ->where('status','gt',1)
                    ->sum('actual_price');
                $storeUsers[$k]['price'] = $price;
                if($price > 0){
                    array_push($array,$storeUsers[$k]);
                }
            }

        } elseif($storeUser['type'] == 3) {
            $store = $storeUser;
            $userInvitation = UserInvitation::where('type','eq',2)
                ->where('inviter','eq',$storeUser['id'])
                ->with('user')
                ->select()->toArray();
            foreach ($userInvitation as $k=>$v){
                $price = ProductOrder::where('user_id','eq',$v['invitee'])
                    ->where('status','gt',1)
                    ->sum('actual_price');
                $userInvitation[$k]['price'] = $price;
                if($price > 0){
                    array_push($array,$userInvitation[$k]);
                }
            }
        }

        if(count($array) > 0) {
            $array = $this->arraySort($array,'price');
        }
        $store['rank'] = $array;
        return $store;
    }

    /**
     * 收入排行
     */
    public function getInviteRankList()
    {
        $uid = TokenService::getCurrentUid();
        $storeUser = StoreUserModel::where('user_id',$uid)->find();
        if($storeUser['type'] == 1){
            $storeUsers = StoreUserModel::where('store_id',$storeUser['store_id'])
                ->with('user')->select()->toArray();
            $money = [];
            foreach ($storeUsers as $k=>$v){
                $invite_money = UserMoneyModel::where('user_id',$storeUsers[$k]['user_id'])
                    ->where('type',1)->with('order')->select();
                if(!$invite_money->isEmpty()) {
                    foreach ($invite_money as $vo){
                        array_push($money,$vo);
                    }
                }
            }
        } elseif($storeUser['type'] == 2) {
            $storeUsers = StoreUserModel::where('group_id',$storeUser['group_id'])
                ->with('user')->select()->toArray();
            $money = [];
            foreach ($storeUsers as $k=>$v){
                $invite_money = UserMoneyModel::where('user_id',$storeUsers[$k]['user_id'])
                    ->where('type',1)->with('order')->select();
                if(!$invite_money->isEmpty()) {
                    foreach ($invite_money as $vo){
                        array_push($money,$vo);
                    }
                }
            }
        } elseif($storeUser['type'] == 3) {
            // 店员收入
            $invite_money = UserMoneyModel::where('user_id',$uid)
                ->with('order')
                ->where('type',1)->order('id desc')->select()->toArray();
        }
        return $invite_money;
    }

    /**
     * 裂变用户列表
     * 1. 查询每个店面的店员信息
     * 2. 查询每个店员所有的下级人员
     * 3. 判断下级用户是否登录手机号
     */
    public function getUserRankList()
    {
        $uid = TokenService::getCurrentUid();
        $storeUser = StoreUserModel::where('user_id',$uid)->find();
        $store = Store::get($storeUser['store_id']);
        $array = [];
        if($storeUser['type'] == 1) {
            $storeUsers = StoreUserModel::where('store_id',$storeUser['store_id'])->with('user')->select()->toArray();
            foreach ($storeUsers as $k=>$v){
                $userInvitation = UserInvitation::where('type','eq',2)
                    ->where('inviter','eq',$v['id'])
                    ->select()->toArray();
                $ids = array_column($userInvitation,'invitee');
                $number = UserModel::where('id','in',$ids)
                    ->where('status','eq',3)
                    ->count();
                $storeUsers[$k]['number'] = $number;
                if($number > 0){
                    array_push($array,$storeUsers[$k]);
                }
            }
        } elseif($storeUser['type'] == 2) {
            $store = StoreGroupModel::where('id',$storeUser['group_id'])->find();
            $storeUsers = StoreUserModel::where('group_id',$storeUser['group_id'])
                ->with('user')
                ->select()
                ->toArray();
            foreach ($storeUsers as $k=>$v){
                $userInvitation = UserInvitation::where('type','eq',2)
                    ->where('inviter','eq',$v['id'])
                    ->select()->toArray();
                $ids = array_column($userInvitation,'invitee');
                $number = UserModel::where('id','in',$ids)->where('status','eq',3)->count();
                $storeUsers[$k]['number'] = $number;
                if($number > 0){
                    array_push($array,$storeUsers[$k]);
                }
            }
        }

        if(count($array) > 0) {
            $array = $this->arraySort($array,'number');
        }
        $store['rank'] = $array;
        return $store;
    }

    /**
     * 成交量
     */
    public function getChengRankList()
    {
        $uid = TokenService::getCurrentUid();
        $storeUser = StoreUserModel::where('user_id',$uid)->with('user')->find();
        $store = Store::get($storeUser['store_id']);
        $array = [];
        if($storeUser['type'] == 1) {
            $storeUsers = StoreUserModel::where('store_id',$storeUser['store_id'])->with('user')->select()->toArray();
            foreach ($storeUsers as $k=>$v){
                $userInvitation = UserInvitation::where('type','eq',2)
                    ->where('inviter','eq',$v['id'])
                    ->select()->toArray();
                $ids = array_column($userInvitation,'invitee');
                $order = ProductOrder::where('user_id','in',$ids)->where('status','gt',1)->select()->toArray();
                $order_ids = array_column($order,'id');
                $number = ProductOrderContent::where('product_order_id','in',$order_ids)->sum('product_number');
                $number_one = ProductOrderContent::where('user_id','eq',$storeUsers[$k]['user_id'])->where('status','gt',1)->sum('product_number');
                $price = $number + $number_one;
                $storeUsers[$k]['number'] = $price;
                if($price > 0){
                    array_push($array,$storeUsers[$k]);
                }
            }
        } elseif($storeUser['type'] == 2) {
            $store = StoreGroupModel::where('id',$storeUser['group_id'])->find();
            $storeUsers = StoreUserModel::where('group_id',$storeUser['group_id'])
                ->with('user')
                ->select()
                ->toArray();
            foreach ($storeUsers as $k=>$v){
                $userInvitation = UserInvitation::where('type','eq',2)
                    ->where('inviter','eq',$v['id'])
                    ->select()->toArray();
                $ids = array_column($userInvitation,'invitee');
                $order = ProductOrder::where('user_id','in',$ids)->where('status','gt',1)->select()->toArray();
                $order_ids = array_column($order,'id');
                $number = ProductOrderContent::where('product_order_id','in',$order_ids)->sum('product_number');
                $number_one = ProductOrderContent::where('user_id','eq',$storeUsers[$k]['user_id'])->where('status','gt',1)->sum('product_number');
                $price = $number + $number_one;
                $storeUsers[$k]['number'] = $price;
                if($price > 0){
                    array_push($array,$storeUsers[$k]);
                }
            }
        }

        if(count($array) > 0) {
            $array = $this->arraySort($array,'number');
        }
        $store['rank'] = $array;
        return $store;
    }

    /**
     * 获取店铺总记录
     */
    public function getAllNumList($start_time,$end_time)
    {
        $where = [];
        if(!$start_time){
            $start_time = strtotime("-1 month");
            $end_time = time();
            $where[] = ['pay_time', 'egt', $start_time];
            $where[] = ['pay_time', 'elt', $end_time];
        } else {
            $where[] = ['pay_time', 'egt', strtotime($start_time)];
            $where[] = ['pay_time', 'elt', strtotime($end_time)];
        }
        $store = Store::order('id desc')->select()->toArray();
        foreach ($store as $k=>$v){
            $whereOne = $where;
            $whereOne[] = ['store_id','eq',$v['id']];
            $orderPrice = ProductOrder::where($whereOne)
                ->where('status','gt','1')->order('id desc')
                ->sum('actual_price');
            $store[$k]['order_price']  = $orderPrice;
        }
        if(count($store) > 0) {
            $store = $this->arraySort($store,'order_price');
        }
        return $store;
    }

    /**
     * 二维数组根据某个字段排序
     * @param array $array 要排序的数组
     * @param string $keys   要排序的键字段
     * @param string $sort  排序类型  SORT_ASC     SORT_DESC
     * @return array 排序后的数组
     */
    public function arraySort($array, $keys, $sort = SORT_DESC) {
        $keysValue = [];
        foreach ($array as $k => $v) {
            $keysValue[$k] = $v[$keys];
        }
        array_multisort($keysValue, $sort, $array);
        return $array;
    }
}